package com.dhemery.mack.application;

import java.util.List;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.dhemery.mack.adapters.ImageDescriptorSource;
import com.dhemery.mack.adapters.NodeAdapterFactory;
import com.dhemery.mack.model.Node;

public class MackPlugin extends AbstractUIPlugin implements ImageDescriptorSource {
	public static final String ID = "com.dhemery.mack";
	public static final String COMMAND_ID_PREFIX = ID + ".command.";
	public static final String EDITOR_ID_PREFIX = ID + ".editor.";
	public static final String PERSPECTIVE_ID_PREFIX = ID + ".perspective.";
	public static final String VIEW_ID_PREFIX = ID + ".view.";
	private static MackPlugin instance;

	public static MackPlugin getInstance() {
		return instance;
	}

	private IAdapterFactory adapterFactory;
	private List<IAction> actions;

	public MackPlugin() {
		super();
	}

	public IAction getAction(String targetId) {
		for(IAction action : actions)
			if(action.getId() == targetId)
				return action;
		return null;
	}

	public IAdapterFactory getAdapterFactory() {
		return adapterFactory;
	}

	public ImageDescriptor getImageDescriptor(String imagePath) {
		return imageDescriptorFromPlugin(ID, imagePath);
	}

	private void registerAdapters(IAdapterFactory factory) {
		Platform.getAdapterManager().registerAdapters(factory, Node.class);
	}

	public void setActions(List<IAction> actions) {
		this.actions = actions;
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		instance = this;
		adapterFactory = new NodeAdapterFactory(this);
		registerAdapters(adapterFactory);
	}
	
	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		instance = null;
	}
}
